package cn.edu.neu.vmmigeval.evaluation;

import cn.edu.neu.vmmigeval.cloud.entity.DataCenter;
import cn.edu.neu.vmmigeval.cloud.entity.Host;
import cn.edu.neu.vmmigeval.io.WriteUtil;

import java.util.List;

/**
 * Created by yfwz100 on 16/2/25.
 */
public class PowerModel {


    private static double a = 66.3098;
    private static double b = 4.7350;
    private static double c = 0.4325;
    private static double d = 0.1283;
    private static double e = 109.4200;


    public double getPower(int clock, DataCenter dc, String url, String name) {

        //double a =2.1,b=0.2,c=0.1,d=0.1,e=0.1;
        List<Host> hosts = dc.getHosts();
        double cpuUtilization = 0, ramUtilization = 0, bwUtilization = 0, diskUtilization = 0;
        double power = 0.0;
        for (Host host : hosts) {

            if (host.getActive() == 0) {
                cpuUtilization = (double) host.getCurrentCpu() / host.getTotalCpu();
                ramUtilization = (double) host.getCurrentRam() / host.getTotalRam();
                bwUtilization = (double) host.getCurrentBw() / host.getTotalBw();
                diskUtilization = (double) host.getCurrentDisk() / host.getTotalDisk();

                if (name.equals("default")) {
                    double r = Math.random();

                    //double rand = r*1.8<0.8?0.8:r;  //  0.8  1.8
                    double rand = r * 2.1 < 0.8 ? 0.8 : r * 2.1;
                    power += (a * cpuUtilization + b * ramUtilization + c * bwUtilization + d * diskUtilization + e) * 36 * rand;
                } else {
                    power += (a * cpuUtilization + b * ramUtilization + c * bwUtilization + d * diskUtilization + e) * 36;
                }
            }
        }

//        String url1 = url + "energy.txt";
//        try {
//            WriteUtil.writePowerToFile(url1, power);
//        } catch (Exception ee) {
//            ee.printStackTrace();
//        }

        return power;
    }


    public static double getPowerOfHost(int clock, Host host) {

        //double a =2.1,b=0.2,c=0.1,d=0.1,e=0.1;

        double cpuUtilization = 0, ramUtilization = 0, bwUtilization = 0, diskUtilization = 0;
        double power = 0;

        if (host.getActive() == 0) {

            cpuUtilization = (double) host.getCurrentCpu() / host.getTotalCpu();
            ramUtilization = (double) host.getCurrentRam() / host.getTotalRam();
            bwUtilization = (double) host.getCurrentBw() / host.getTotalBw();
            diskUtilization = (double) host.getCurrentDisk() / host.getTotalDisk();

            power = a * cpuUtilization + b * ramUtilization + c * bwUtilization + d * diskUtilization + e;
        }


        return power;
    }

    public static double getPowerOfHostByCpuOR(double cpu, double ram, double bw, double disk) {

        //	double a =2.1,b=0.2,c=0.1,d=0.1,e=0.1;

        double power = 0;
        power = a * cpu + b * ram + c * bw + d * disk + e;

        return power;
    }

	/*
	public static void main(String[] args) {

		double r = Math.random();
		System.out.println(r);
		double rand = r*1.8<0.8?0.8:r*2.1;
		System.out.println(rand);

	}
	*/

}